package com.wang.nowcoder.controller;

import com.wang.nowcoder.pojo.Comment;
import com.wang.nowcoder.pojo.DiscussPost;
import com.wang.nowcoder.pojo.Page;
import com.wang.nowcoder.pojo.User;
import com.wang.nowcoder.service.CommentService;
import com.wang.nowcoder.service.DiscussPostService;
import com.wang.nowcoder.service.LikeService;
import com.wang.nowcoder.service.UserService;
import com.wang.nowcoder.util.CommunityConstant;
import com.wang.nowcoder.util.CommunityUtil;
import com.wang.nowcoder.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author wang
 * @ClassName DiscussPostController
 * @Description TODO
 * @date 2021/12/23 9:29
 * @Version 1.0
 */
@Controller
@RequestMapping("/discuss")
public class DiscussPostController implements CommunityConstant {

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private UserService userService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private LikeService likeService;

    @PostMapping("/addDiscuss")
    @ResponseBody
    public String addDiscussPost(String title, String content) {
        User user = hostHolder.getUser();
        if (user == null) {
            return CommunityUtil.getJSONString(403, "您还没有登录");
        }

        DiscussPost post = new DiscussPost();
        post.setUserId(user.getId());
        post.setTitle(title);
        post.setContent(content);
        post.setCreateTime(new Date());
        discussPostService.addDiscussPost(post);
//        报错的情况会统一处理
        return CommunityUtil.getJSONString(0, "发布成功");
    }

    @RequestMapping("/detail/{discussPostId}")
    public String selectDiscussById(@PathVariable("discussPostId") int discussPostId, Model model, Page page) {
        //帖子
        DiscussPost post = discussPostService.selectDiscussPostByID(discussPostId);
        System.out.println(post);
        model.addAttribute("postDetail", post);
        //作者
        System.out.println(post.getUserId());
        System.out.println(post.getId());
        User user = userService.selectById(post.getUserId());
        System.out.println(user);
        model.addAttribute("user", user);
        //点赞
        long likeCount = likeService.selectPojoLikeCount(ENTITY_TYPE_POST, discussPostId);
        model.addAttribute("likeCount", likeCount);
        //点赞状态
        int likeStatus = hostHolder.getUser() == null ? 0 : likeService.selectPojoLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_POST, discussPostId);
        model.addAttribute("likeStatus", likeStatus);

        //查评论的分页信息
        page.setLimit(5);
        page.setPath("/discuss/detail/" + discussPostId);
        page.setRows(post.getCommentCount());

        //评论:给帖子的评论
        //回复：给评论的回复
        //评论列表
        List<Comment> commentList = commentService.selectCommentByEntity
                (ENTITY_TYPE_POST, post.getId(), page.getOffset(), page.getLimit());
        System.out.println(commentList);
        //评论Vo列表
        List<Map<String, Object>> commentVoList = new ArrayList<>();
        if (commentList != null) {
            for (Comment comment : commentList) {
                //评论Vo
                Map<String, Object> commentVo = new HashMap<>();
                //评论
                commentVo.put("comment", comment);
                //作者
                commentVo.put("user", userService.selectById(comment.getUserId()));
                System.out.println(userService.selectById(comment.getUserId()));

                //点赞数量
                likeCount = likeService.selectPojoLikeCount(ENTITY_TYPE_COMMENT,comment.getId());
                commentVo.put("likeCount",likeCount);
                //点赞状态
                likeStatus = hostHolder.getUser() == null ? 0 : likeService.selectPojoLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_COMMENT, comment.getId());
                commentVo.put("likeStatus", likeStatus);

                //回复列表
                List<Comment> replyList = commentService.selectCommentByEntity
                        (ENTITY_TYPE_COMMENT, comment.getId(), 0, Integer.MAX_VALUE);
                //回复Vo列表
                List<Map<String, Object>> replyVoList = new ArrayList<>();
                if (replyList != null) {
                    for (Comment reply : replyList) {
                        Map<String, Object> replyVo = new HashMap<>();
                        //回复
                        replyVo.put("reply", reply);
                        //回复作者
                        System.out.println(userService.selectById(reply.getUserId()));
                        replyVo.put("user", userService.selectById(reply.getUserId()));
                        //回复目标
                        User target = reply.getTargetId() == 0 ? null : userService.selectById(reply.getTargetId());
                        replyVo.put("target", target);
                        //点赞数量
                        likeCount = likeService.selectPojoLikeCount(ENTITY_TYPE_COMMENT,reply.getId());
                        replyVo.put("likeCount",likeCount);
                        //点赞状态
                        likeStatus = hostHolder.getUser() == null ? 0 : likeService.selectPojoLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_COMMENT, reply.getId());
                        replyVo.put("likeStatus", likeStatus);
                        replyVoList.add(replyVo);
                    }
                }
                commentVo.put("replys", replyVoList);

                //回复数量
                int replyCount = commentService.selectCountByEntity(ENTITY_TYPE_COMMENT, comment.getId());
                commentVo.put("replyCount", replyCount);

                commentVoList.add(commentVo);
            }
        }
        System.out.println(commentVoList);
        model.addAttribute("comments", commentVoList);

        return "/site/discuss-detail";
    }

}
